package de.hsb.ai.agent.TeamDetectiveAgent;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;


public class PersistenceManager {

	private BufferedWriter out;
	private BufferedReader in;
	private String dateiName;
	
	public PersistenceManager(){
		dateiName = "moveDistances.txt";
	}
	
	public void save(Vector<String> v) throws IOException{
		out = new BufferedWriter(new FileWriter(this.dateiName));	
		for(int i = 0; i < v.size(); i++) {
			System.out.println("PERSISTENCE "+v.get(i));
			out.write(v.get(i) + "");
		}
		out.close();			
	}
	
	public int load(int from, int to) throws IOException{
		in = new BufferedReader(new FileReader(this.dateiName));
		String s;
		int counter = 1;
		while((s = in.readLine()) != null) {
			if(counter==from){
				in.close();
//				System.out.println(s);
				return getStringNumber(s, to);
			}
			counter++;
		}
		in.close();
		return 1000;
	}
	
	private int getStringNumber(String s, int to){
		int counter = 1;
		for (int i = 0; i < s.length(); i++) {
			if(s.charAt(i) == ';'){
				counter++;
			}
			if(counter == to && s.charAt(i) != ';'){
				if(s.charAt(i+1) == ';'){
					String toStr = ""+s.charAt(i);
					int toInt = Integer.parseInt(toStr);
					return toInt;
				}else{
					String toStr = ""+s.charAt(i)+s.charAt(i+1);
					int toInt = Integer.parseInt(toStr);
					return toInt;
				}
			}
		}
		System.out.println("string"+s.length());
		return 1000;
	}
}









